package com.cgbl.cg.ui.mine

import android.graphics.Color
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppActivityPersonalProfileBinding
import com.cgbl.cg.view.*
import com.cgbl.cg.vm.mine.PersonalProfileViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import java.util.*
import android.view.WindowManager





@Route(path = AppConstants.Router.Mine.A_MINE_PROFILE)
class PersonalProfileActivity :
    GLBaseActivity<AppActivityPersonalProfileBinding, PersonalProfileViewModel>() {

    private lateinit var updateUserNamePop: UpdateUserNamePop

    private lateinit var updateGenderPop: UpdateGenderPop

    private lateinit var updateJobPop: UpdateJobPop

    private lateinit var updateSignaturePop: UpdateSignaturePop

    private lateinit var updateCityPop: UpdateCityPop

    private lateinit var pvTime: CGTimePickerView

    override fun initComponents() {
        initTiterBar()
        initPop()
        initTimePicker()
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView.text = "基本信息"
        backView.setOnClickListener { finish() }
    }

    private fun initPop() {
        updateUserNamePop = UpdateUserNamePop(this)
        updateGenderPop = UpdateGenderPop(this)
        updateJobPop = UpdateJobPop(this)
        updateSignaturePop = UpdateSignaturePop(this)
        updateCityPop = UpdateCityPop(this)
    }

    override fun initViewObservable() {
        viewModel.uc.showUpdateUserName.observe(this) {
            XPopup.Builder(this)
                .moveUpToKeyboard(false)
                .asCustom(updateUserNamePop)
                .show()
        }
        viewModel.uc.showUpdateGender.observe(this) {
            XPopup.Builder(this)
                .asCustom(updateGenderPop)
                .show()
        }
        viewModel.uc.showUpdateJob.observe(this) {
            XPopup.Builder(this)

                .asCustom(updateJobPop)
                .show()
        }
        viewModel.uc.showUpdateSignature.observe(this) {
            XPopup.Builder(this)
                .moveUpToKeyboard(false)
                .asCustom(updateSignaturePop)
                .show()
        }
        viewModel.uc.showUpdateBirthday.observe(this) {
            pvTime.show()
        }
        viewModel.uc.showUpdateAddress.observe(this) {
            XPopup.Builder(this)
                .asCustom(updateCityPop)
                .show()
        }

    }

    private fun initTimePicker() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        val selectedDate: Calendar = Calendar.getInstance()
        //时间选择器
        pvTime = CGTimePickerBuilder(
            this
        ) { date, v -> //选中事件回调
            // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
            /*btn_Time.setText(getTime(date));*/
//            val btn: Button = v as Button
//            btn.setText(getTime(date))
        }
            .setLayoutRes(
                R.layout.app_pickerview_birthday
            ) { v ->
//                val tvSubmit = v.findViewById(R.id.tv_finish) as TextView
//                val ivCancel = v.findViewById(R.id.iv_cancel) as ImageView
//                tvSubmit.setOnClickListener {
//                    pvTime.returnData()
//                    /*pvTime.dismiss();*/
//                }
//                ivCancel.setOnClickListener { /*pvTime.dismiss();*/ }
            }
            .setType(booleanArrayOf(true, true, true, false, false, false))
            .setLabel("", "", "", "", "", "") //设置空字符串以隐藏单位提示   hide label
            .setDividerColor(Color.WHITE)
            .setContentTextSize(14)
            .setTextColorCenter(Color.parseColor("#424242"))
            .setTextColorOut(Color.parseColor("#7e7e7e"))
            .setDate(selectedDate)
            .setOutSideColor(0x00000000)
            .setLineSpacingMultiplier(3f)
            .setItemVisibleCount(4)
            .setOutSideCancelable(false)
            .isDialog(true)
            .build()
        pvTime.setKeyBackCancelable(false) //系统返回键监听屏蔽掉
    }


    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_personal_profile

    override fun initVariableId(): Int = BR.viewModel

}